//
//  MineAssessViewController.m
//  renmingongshe
//
//  Created by Mao on 2017/10/30.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "MineAssessViewController.h"
#import "TggStarEvaluationView.h"
#import "SelectPictureView.h"

@interface MineAssessViewController ()<UITextViewDelegate>
{
    NSInteger _goodsNum;
    NSInteger _serviceNum;
    NSInteger _speedNum;
    
    NSMutableArray *_images;
    NSDictionary *_data;
}
@property (strong,nonatomic)    UILabel *placeholder;

@property (strong, nonatomic) SelectPictureView *pictureView;
@property (weak, nonatomic) IBOutlet UIView *secBg;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIImageView *goodsIma;
@property (weak, nonatomic) IBOutlet UIView *tagStarView;
@property (weak, nonatomic) IBOutlet UIView *serviceStar;
@property (weak, nonatomic) IBOutlet UIView *speedStar;

@end

@implementation MineAssessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"我的订单";
    [_goodsIma sd_setImageWithURL:[NSURL URLWithString:[Utils getImaSting:self.goodsImg]] placeholderImage:[UIImage imageNamed:@"NoStore"] options:SDWebImageRetryFailed];
    self.view.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    _images = [NSMutableArray array];
    [self initView];
}

- (UILabel *)placeholder
{
    if (!_placeholder) {
        _placeholder = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
        _placeholder.font = [UIFont systemFontOfSize:14];
        _placeholder.textColor = [UIColor lightGrayColor];
        _placeholder.text = @"请填写对此商品的评价";
    }
    return _placeholder;
}
- (void)uploadIma:(NSData *)data
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
    
    NSDictionary *dic = @{@"name":@"file",@"type":@"image/png",@"tmp_name":fileName,@"size":@(data.length)};
    NSDictionary *para = @{@"upload_file":@[dic], @"userToken":[Utils getUserToken]};
    [THWManager PostImageWithData:API(MineAssessAddImg) imageData:data otherParamters:para success:^(NSURLSessionDataTask *task, id responseObject) {
        [_images addObject:responseObject[@"data"][@"msg"]];
        [self.view removeAnyView];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
- (void)initView
{
    [self.textView addSubview:self.placeholder];
    
    _pictureView = [[SelectPictureView alloc]initWithFrame:CGRectMake(0, 130*SCALY, WIDTH, 80)];
    [_secBg addSubview:_pictureView];
    self.pictureView.maxImageCount = 3;
    self.pictureView.ratioOfWidthAndHeight = 240.0/240.0;
    __weak typeof(self) weakSelf = self;
    self.pictureView.getImageBlock = ^(NSMutableArray *ImageArr){
        
        NSLog(@"ImageArr ======= %lu", (unsigned long)ImageArr.count);
        weakSelf.pictureView.height = 80;
        [weakSelf.view loadingOnAnyView];
        for (NSDictionary *dic in ImageArr) {
            if ([dic[@"image"] isKindOfClass:[NSData class]]) {
                [weakSelf uploadIma:dic[@"image"]];
            }
        }
    };
    
    // 初始化
    TggStarEvaluationView *tggStarEvaView = [TggStarEvaluationView evaluationViewWithChooseStarBlock:^(NSUInteger count) {
        NSLog(@"\n\n给了铁哥哥：%ld星好评！！!\n\n",count);
        _goodsNum = count;
    }];
    
    tggStarEvaView.frame = CGRectMake(0, 0, 200*SCALX, 40);
    [self.tagStarView addSubview:tggStarEvaView];
    
    TggStarEvaluationView *serviceStarEvaView = [TggStarEvaluationView evaluationViewWithChooseStarBlock:^(NSUInteger count) {
        NSLog(@"\n\n给了铁哥哥：%ld星好评！！!\n\n",count);
        _serviceNum = count;
    }];
    serviceStarEvaView.frame = CGRectMake(0, 0, 200*SCALX, 40);
    
    [self.serviceStar addSubview:serviceStarEvaView];
    
    TggStarEvaluationView *speedStarEvaView = [TggStarEvaluationView evaluationViewWithChooseStarBlock:^(NSUInteger count) {
        NSLog(@"\n\n给了铁哥哥：%ld星好评！！!\n\n",count);
        _speedNum = count;
    }];
    speedStarEvaView.frame = CGRectMake(0, 0, 200*SCALX, 40);
    
    [self.speedStar addSubview:speedStarEvaView];
}
- (IBAction)confirmClick:(id)sender {
    if (!_goodsNum) {
        [self.view Message:@"请评价商品"];
        return;
    }
    if (!_serviceNum) {
        [self.view Message:@"请评价服务"];
        return;
    }
    if (!_speedNum) {
        [self.view Message:@"请评价物流"];
        return;
    }
    NSString *str;
    NSDictionary *para;
    if ([_type isEqualToString:@"shop"]) {
        str =API(MineLOAssessClick);
    }else{
        str =API(MineLDAssessClick);
        para = @{@"orderId":_orderId, @"goodsId":self.goodsId,@"goodsScore":@(_goodsNum),@"timeScore":@(_speedNum),@"serviceScore":@(_serviceNum),@"content":_textView.text,@"images":_images,@"userToken":[Utils getUserToken]};
    }
    [THWManager PostHttp:str Parameters:para Success:^(id responseObject) {
        
        if([responseObject[@"status"]integerValue]){
            [self.view Message:responseObject[@"msg"]];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }
    } andFalse:^(NSError *error) {
        
    }];
}
#pragma mark textview
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self.placeholder removeFromSuperview];
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (!textView.text.length) {
        [textView addSubview:self.placeholder];
    }else{
        [self.placeholder removeFromSuperview];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_textView resignFirstResponder];
}
-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
